//
//  LXRequest.m
//  ZuLinProject
//
//  Created by 谭明星 on 2021/12/6.
//

#import "LXRequest.h"

@interface LXGetRequest ()
@property (nonatomic, copy) NSString *reqUrl;
@property (nonatomic, assign) BOOL hasHeader;
@property (nonatomic, assign) BOOL useCache;
@property (nonatomic, assign) NSTimeInterval timeout;
@property (nonatomic, copy)   NSString * cmd;
@property (nonatomic, strong) NSDictionary * oriParam;//用于存储缓存的key
@property (nonatomic, strong) NSDictionary * param;
@end

@implementation LXGetRequest

+ (instancetype)reqNOEncryptAndHeaderParam:(NSMutableDictionary *)param UrlString:(NSString*)url {
    NSDictionary *dic = [[NSDictionary alloc] initWithDictionary:param];
    return [[self alloc] initWithURL:url encrypt:NO header:YES param:dic timeout:30 cache:NO];
}

- (instancetype)initWithURL:(NSString *)url encrypt:(BOOL)encrypt header:(BOOL)header param:(NSDictionary *)param timeout:(NSTimeInterval)timeout cache:(BOOL)cache{
    self = [super init];
    if (self){
        self.ignoreCache = cache;
        self.useCache = cache;
        self.reqUrl = url;
        NSLog(@"<-- %s, %d --> 👉👉👉👉👉👉reqUrl👈👈👈👈👈👈%@ 👉👉👉👉👉👉param👈👈👈👈👈👈%@", __FUNCTION__, __LINE__, url, param);
        self.oriParam = param?param:@{};
        if (param.count > 0) {
            self.cmd = param[@"password"]?param[@"password"]:@"";
            if (encrypt) {
                self.param = [self encryptParams:param];
                if(self.param == nil){
                    self.tag = 1;
                } else {
                    self.tag = 0;
                }
            } else {
                self.param = [self noEncryptParams:param];
            }
        } else {
            self.param = [self noEncryptParams:param];
        }
        self.hasHeader = header;
        self.timeout = timeout;
    }
    return self;
}

- (id)requestArgument{
    return _param;
}

- (nullable id)reqOriID {
    return _oriParam;
}

- (NSString *)requestUrl{
    return _reqUrl;
}

- (NSInteger)cacheTimeInSeconds{
    if (_useCache){
        return 60 * 60 * 24;
    } else {
        return -1;
    }
}

- (YTKRequestMethod)requestMethod {
    return  YTKRequestMethodGET;
}

- (NSTimeInterval)requestTimeoutInterval {
    return _timeout;
}

- (NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary {
    if (self.hasHeader) {
        @synchronized (self) {
            NSMutableDictionary * header = [@{} mutableCopy];
            YLUserInfo *InfoModel = [YLUserMgr fetchUser];
            
            if (InfoModel.authorization.length > 0) {
                header[@"Authorization"] = [NSString stringWithFormat:@"Bearer %@",InfoModel.authorization];
            } else {
                header[@"Authorization"] = @"Basic dGVzdDp0ZXN0";
            }
            header[@"appVersion"] = @"1.0.0";   //版本号
//            header[@"device"] = @"iOS";
            if (InfoModel.loginNo.length > 0){
                header[@"account_no"] = InfoModel.loginNo;
            }
            NSLog(@"<-- %s, %d --> 🐔header🐔%@", __FUNCTION__, __LINE__, header);
            return header;
        }
    } else {
        return nil;
    }
}

- (YTKRequestSerializerType)requestSerializerType {
    return YTKRequestSerializerTypeHTTP;
}

- (YTKResponseSerializerType)responseSerializerType {
    return YTKResponseSerializerTypeHTTP;
}

@end

@interface LXPostRequest ()

@property (nonatomic, copy) NSString *reqUrl;
@property (nonatomic, assign) BOOL hasHeader;
@property (nonatomic, assign) BOOL useCache;
@property (nonatomic, assign) NSTimeInterval timeout;
@property (nonatomic, copy)   NSString * cmd;
@property (nonatomic, strong) NSDictionary * oriParam;//用于存储缓存的key
@property (nonatomic, strong) NSDictionary * param;

@end

@implementation LXPostRequest

+ (instancetype)reqNOEncryptAndHeaderParam:(NSMutableDictionary *)param UrlString:(NSString*)url{
    NSDictionary   *  dic =[[NSDictionary alloc] initWithDictionary:param];
    return [[self alloc] initWithURL:url encrypt:NO header:YES param:dic timeout:30 cache:NO];
}

- (instancetype)initWithURL:(NSString *)url encrypt:(BOOL)encrypt header:(BOOL)header param:(NSDictionary *)param timeout:(NSTimeInterval)timeout cache:(BOOL)cache{
    self = [super init];
    if (self){
        self.ignoreCache = cache;
        self.useCache = cache;
        self.reqUrl = url;
        NSLog(@"<-- %s, %d --> 👉👉👉👉👉👉reqUrl👈👈👈👈👈👈%@ 👉👉👉👉👉👉param👈👈👈👈👈👈%@", __FUNCTION__, __LINE__, url, param);
        self.oriParam = param?param:@{};
        if (param.count > 0) {
            self.cmd = param[@"password"]?param[@"password"]:@"";
            if (encrypt) {
                self.param = [self encryptParams:param];
                if(self.param == nil){
                    self.tag = 1;
                } else {
                    self.tag = 0;
                }
            } else {
                self.param = [self noEncryptParams:param];
            }
        } else {
            self.param = [self noEncryptParams:param];
        }
        self.hasHeader = header;
        self.timeout = timeout;
    }
    return self;
}

- (id)requestArgument{
    return _param;
}

- (nullable id)reqOriID {
    return _oriParam;
}

- (NSString *)requestUrl{
    return _reqUrl;
}

- (NSInteger)cacheTimeInSeconds{
    if (_useCache){
        return 60 * 60 * 24;
    } else {
        return -1;
    }
}

- (YTKRequestMethod)requestMethod {
    return  YTKRequestMethodPOST;
}

- (NSTimeInterval)requestTimeoutInterval {
    return _timeout;
}

- (NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary {
    if (self.hasHeader) {
        @synchronized (self) {
            NSMutableDictionary * header = [@{} mutableCopy];
            YLUserInfo *InfoModel = [YLUserMgr fetchUser];
            if (InfoModel.authorization.length > 0) {
                header[@"Authorization"] = [NSString stringWithFormat:@"Bearer %@",InfoModel.authorization];
            } else {
                header[@"Authorization"] = @"Basic dGVzdDp0ZXN0";
            }
            if (InfoModel.loginNo.length > 0){
                header[@"account_no"] = InfoModel.loginNo;
            }
            header[@"appVersion"] = @"";   //版本号
//            header[@"device"] = @"iOS";
            NSLog(@"<-- %s, %d --> 🐔header🐔%@", __FUNCTION__, __LINE__, header);
            return header;
        }
    } else {
        return nil;
    }
}

- (YTKRequestSerializerType)requestSerializerType {
    return YTKRequestSerializerTypeHTTP;
}

- (YTKResponseSerializerType)responseSerializerType {
    return YTKResponseSerializerTypeHTTP;
}

@end
